package com.lbm.business.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.lbm.baseclass.controller.LBMBaseController;
import com.lbm.baseclass.utils.json.JsonUtil;
import com.lbm.business.service.IModuleInfoService;
import com.lbm.business.service.IModuleOperateService;
import com.lbm.model.ModuleInfo;
import com.lbm.model.ModuleOperate;
import com.lbm.model.RoleInfo;
import com.lbm.model.RoleModule;

public class ModuleOperateController extends LBMBaseController{
	private IModuleOperateService moduleOperateService;
	private IModuleInfoService moduleInfoService;

	public void setModuleInfoService(IModuleInfoService moduleInfoService) {
		this.moduleInfoService = moduleInfoService;
	}
	public void setModuleOperateService(IModuleOperateService moduleOperateService) {
		this.moduleOperateService = moduleOperateService;
	}
	/**
	* 模块操作列表
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/moduleoperate/moduleoperatelist.jsp");
	    return mv;
	}
	/**
	* 加载模块操作列表数据
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView ajaxlist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String moduleid=request.getParameter("moduleid");
		String hql="from ModuleOperate where moduleid='"+moduleid+"'";
		List<ModuleOperate> list=calcPage(request, moduleOperateService, hql);
		int total=moduleOperateService.getDataTotalNum(hql);
		String json="";
		json = "{\"total\": " + total + ", \"rows\": "
				+ JsonUtil.toString(list, JsonUtil.COLLECTION_FILTER) + "}";
		renderJson(response, json);
	    return null;
	}
	/**
	* 添加
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/moduleoperate/moduleoperateform.jsp");
		mv.addObject("moduleid", request.getParameter("moduleid"));
		return mv;
	}
	/**
	* 修改
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView modify(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/moduleoperate/moduleoperateform.jsp");
		ModuleOperate moduleOperate=moduleOperateService.getModuleOperateById(request.getParameter("id"));
		mv.addObject("moduleOperate", moduleOperate);
		mv.addObject("moduleid", moduleOperate.getModuleid());
		return mv;
	}
	/**
	* 删除
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		boolean flag=true;
		String id=request.getParameter("ids");
		String[] ids=id.split(",");
		for(int i=0;i<ids.length;i++){
			try {
				ModuleOperate module = moduleOperateService.getModuleOperateById(ids[i]);
				if (module != null)
					moduleOperateService.deleteModuleOperate(ids[i]);
			} catch (Exception e) {
				log.error(e);
				flag=false;
			}
		}
		renderJson(response, toJson("success", flag));
		return null;
	}
	/**
	* 保存
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		boolean flag=true;
		String moduleid=request.getParameter("moduleid");
		String operatename=request.getParameter("operateName");
		String operateCode=request.getParameter("operateCode");
		String moduleOperateid=request.getParameter("moduleOperateid");
		ModuleInfo module=moduleInfoService.getModuleInfoById(moduleid);
		ModuleOperate operate=new ModuleOperate();
		operate.setModuleOperateid(moduleOperateid);
		operate.setOperateName(operatename);
		operate.setOperateCode(operateCode);
		operate.setModuleid(moduleid);
		operate.setModuleCode(module.getModuleCode());
		List<RoleInfo> roleinfo=baseService.getValueObjectsByHQL("from RoleInfo where roleName ='超级管理员'");
		RoleModule rolemodule=new RoleModule();
		try {
			moduleOperateService.saveOrUpdateModuleOperate(operate);
			rolemodule.setModuleid(moduleid);
			rolemodule.setOperateid(operate.getModuleOperateid());
			rolemodule.setRoleid(roleinfo.get(0).getRoleid());
			baseService.saveOrUpdateVO(rolemodule);
		} catch (Exception e) {
			log.error(e);
			flag=false;
		}
		renderJson(response, toJson("success",flag));
		return null;
	}
}
